package com.lfg.leetcode.leetcode500;


import java.util.ArrayList;
import java.util.List;

public class No969pancakeSort {
    public static void main(String[] args) {
        System.out.println(pancakeSort(new int[]{3, 2, 4, 1}));

    }

    public static List<Integer> pancakeSort(int[] arr) {
        List<Integer> list = new ArrayList<>();
        for (int i = arr.length - 1; i > 0; i--) {
            int index = 0;
            for (int n = i; n > 0; n--) {
                if (arr[index] < arr[n]) {
                    index = n;
                }
            }
            if (index == i) {
                continue;
            }
            reverse(arr, 0, index);
            reverse(arr, 0, i);
            list.add(index+1);
            list.add(i+1);
        }
        return list;
    }

    private static void reverse(int[] list, int i, int j) {
        while (i < j) {
            int num = list[i];
            list[i] = list[j];
            list[j] = num;
            i++;
            j--;
        }
    }


}



